#!/usr/bin/env bash
root="bin/nvim-bin"
get_tag() {
  curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
    grep '"tag_name":' |                                            # Get tag line
    sed -E 's/.*"([^"]+)".*/\1/'                                    # Pluck JSON value
}

get_latest_release() {
  v=$(get_tag "$1")
  if [[ -z "$v" ]]; then
    exit
  fi
  echo "${v:1}"
}
# nvim_version=$(get_latest_release "neovim/neovim")
# shfmt_version=$(get_latest_release "patrickvane/shfmt" )
os=$(uname)
platform=macos
if [[ $os == "Linux" ]]; then
  platform=linux
fi
if [[ -d $root ]]; then
  echo "$root" exists
else
  mkdir -p $root
fi

pushd $root || exit
ln_lazygit() {
  version=$(get_latest_release "jesseduffield/lazygit")
  platform=$1
  file=lazygit_"$version"_$platform
  url=https://github.com/jesseduffield/lazygit/releases/download/v$version/$file.tar.gz
  # https://github.com/jesseduffield/lazygit/releases/download/v0.40.2/lazygit_0.40.2_Darwin_x86_64.tar.gz
  if [[ -f $file.tar.gz ]]; then
    echo "$file" exists
  else
    wget "$url"
  fi
  if [[ ! -d lazygit64 ]]; then
    mkdir lazygit64
  fi
  pushd lazygit64 || exit
  tar xzvf ../"$file".tar.gz
  chmod +x lazygit
  popd || exit
  rm -f lazygit
  if [[ -f lazygit64/lazygit ]]; then
    ln -s lazygit64/lazygit lazygit
  fi
}
ln_codeminmap() {
  version=$(get_latest_release "wfxr/code-minimap")
  platform=$1
  file=code-minimap-v"$version"-$platform
  if [[ -d $file ]]; then
    echo "$file" exists
  else
    wget https://github.com/wfxr/code-minimap/releases/download/v"$version"/"$file".tar.gz
    tar xzvf "$file.tar.gz"
    # https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep-13.0.0-x86_64-apple-darwin.tar.gz
  fi
  rm -f code-minimap
  ln -s "$file"/code-minimap code-minimap
}

ln_ripgrep() {
  version=$(get_tag "BurntSushi/ripgrep")
  platform=$1
  file=ripgrep-$version-$platform
  if [[ -f $file.tar.gz ]]; then
    echo "$file" exists
  else
    wget https://github.com/BurntSushi/ripgrep/releases/download/"$version"/"$file".tar.gz
    tar xzvf "$file".tar.gz
    # https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep-13.0.0-x86_64-apple-darwin.tar.gz
  fi
  chmod +x "$file"/rg
  rm -f rg
  ln -s "$file"/rg rg
}
ln_shfmt() {
  platform=$1
  file=shfmt_$platform
  if [[ -f $file ]]; then
    echo "$file" exists
  else
    wget https://github.com/patrickvane/shfmt/releases/download/master/"$file"
  fi

  chmod +x "$file"
  rm -f shfmt
  ln -s "$file" shfmt
}
ln_tree_sitter() {
  version=$(get_latest_release "tree-sitter/tree-sitter")
  platform=$1
  file=tree-sitter-$platform
  if [[ -f $file ]]; then
    echo "$file" exists
  else
    wget https://github.com/tree-sitter/tree-sitter/releases/download/v"$version"/"$file".gz
    gzip -d "$file".gz
  fi
  chmod +x "$file"
  rm -f tree-sitter
  ln -s "$file" tree-sitter
}
ln_rr() {
  platform=$1
  # https://github.com/rr-debugger/rr/releases/download/5.7.0/rr-5.7.0-Linux-x86_64.tar.gz
  version=$(get_tag "rr-debugger/rr")
  file=rr-$version-$platform
  if [[ -f $file.tar.gz ]]; then
    echo "$file"
  else
    wget https://github.com/rr-debugger/rr/releases/download/"$version"/"$file.tar.gz"
    tar xzvf "$file.tar.gz"
  fi
  chmod +x "$file"/bin/rr
  rm -f rr
  ln -s "$file"/bin/rr rr
}
ln_glow() {
  # https://github.com/charmbracelet/glow/releases/download/v1.5.1/glow_Linux_x86_64.tar.gz
  platform=$1
  version=$(get_latest_release "charmbracelet/glow")
  file=glow_$platform.tar.gz
  if [[ -f $file ]]; then
    echo "$file" exists
  else
    wget https://github.com/charmbracelet/glow/releases/download/v"$version"/"$file"
    tar xzvf "$file"
  fi
  # chmod +x glow
  # ln -s glow glow
}
ln_fd() {
  platform=$1
  version=$(get_latest_release "sharkdp/fd")
  # https://github.com/sharkdp/fd/releases/download/v8.7.0/fd-v8.7.0-i686-unknown-linux-musl.tar.gz
  file=fd-v"$version"-"$platform"
  url=https://github.com/sharkdp/fd/releases/download/v"$version"/"$file.tar.gz"
  if [[ -f $file.tar.gz ]]; then
    echo "$file" exists
  else
    wget "$url"
    tar xzvf "$file.tar.gz"
  fi
  rm -f fd
  ln -s "$file"/fd fd
}
ln_nvim() {
  linux64=$1
  file=nvim-$linux64
  if [[ -f "$file.tar.gz" ]]; then
    echo "$file"
  else
    wget https://github.com/neovim/neovim/releases/download/stable/$file.tar.gz
    tar xzvf "$file".tar.gz
  fi
  rm -f nvim
  ln -s "$file"/bin/nvim nvim
}
if [[ "$platform" == "macos" ]]; then
  ln_fd x86_64-apple-darwin
  ln_nvim macos
  ln_glow Darwin_x86_64
  ln_tree_sitter macos-x64
  ln_shfmt darwin_amd64
  ln_ripgrep x86_64-apple-darwin
  ln_lazygit Darwin_x86_64
  ln_codeminmap x86_64-apple-darwin

# tar xzvf ripgrep-13.0.0-x86_64-apple-darwin.tar.gz
fi

if [[ "$platform" == "linux" ]]; then
  ln_rr Linux-x86_64
  ln_fd i686-unknown-linux-musl
  ln_glow Linux_x86_64
  ln_nvim linux-x86_64
  ln_tree_sitter linux-x64
  ln_shfmt linux_amd64
  ln_ripgrep x86_64-unknown-linux-musl
  ln_lazygit Linux_x86_64
  ln_codeminmap i686-unknown-linux-gnu
fi
popd || exit
